<?php
/* 


/-----------------------------------------------\
|          CODE by Kirati Isarapungpun          |
|       For North - Chiang Mai University       |
|       Software Engineering Project 2013       |
|                                               |
|  |-|>===================================<|-|  |
|                                               |
|       Course planning for Bachelor of         |
| Science degree in software engineering 2012   | 
|      Faculty of Science and Technology,       |
|        North - Chiang Mai University          |
|                                               |
\-----------------------------------------------/

/												\
			Plan	:	Class Plan    
\												/

*/
	//error_reporting(0);
	class Plan
	{
		private $id;
		private $sId;
		private $uId;
		private $year;
		private $term;
		private $grade;
		private $gradePlan;

		function __construct()
		{
			
		}
		
		function getID()
		{
			return $this->id;
		}
		
		function setID($set)
		{
			$this->id = $set;
		}
		
		function getSID()
		{
			return $this->sId;
		}
		
		function setSID($set)
		{
			$this->sId = $set;
		}
		
		function getUID()
		{
			return $this->uId;
		}
		
		function setUID($set)
		{
			$this->uId = $set;
		}
		
		function getYear()
		{
			return $this->year;
		}
		
		function setYear($set)
		{
			$this->year = $set;
		}
		
		function getTerm()
		{
			return $this->term;
		}
		
		function setTerm($set)
		{
			$this->term = $set;
		}
		
		function getGrade()
		{
			return $this->grade;
		}
		
		function setGrade($set)
		{
			$this->grade = $set;
		}
		
		function getGradePlan()
		{
			return $this->gradePlan;
		}
		
		function setGradePlan($set)
		{
			$this->gradePlan = $set;
		}
		
		
		function setAll($id,$sId,$uId,$year,$term,$grade,$gradePlan)
		{
			$this->id = $id;
			$this->sId = $sId;
			$this->uId = $uId;
			$this->year = $year;
			$this->term = $term;
			$this->grade = $grade;
			$this->gradePlan = $gradePlan;
		}
		
		function findOne()
		{
			$query = mysql_query('SELECT * FROM plan WHERE P_id = "'.$this->id.'" LIMIT 1')or die(mysql_error());
			$arr = mysql_fetch_object($query);
			return($arr->S_name);
		}
		
		function findOnePre()
		{
			$query1 = mysql_query('SELECT COUNT(`P_grade`) AS rowcount FROM `plan` WHERE `P_S_id` =  "'.$this->getSID().'" AND `P_year` <= "'.$this->getYear().'" AND `P_term` < "'.$this->getTerm().'" AND `P_U_id` = "'.$this->getUID().'" ORDER BY `P_year` DESC, `P_term` DESC LIMIT 1')or die(mysql_error());
			$arr1 = mysql_fetch_object($query1);
			if($arr1->rowcount > 0)
			{
			$query2 = mysql_query('SELECT `P_grade` FROM `plan` WHERE `P_S_id` =  "'.$this->getSID().'" AND `P_year` <= "'.$this->getYear().'" AND `P_term` < "'.$this->getTerm().'" AND `P_U_id` = "'.$this->getUID().'" ORDER BY `P_year` DESC, `P_term` DESC LIMIT 1')or die(mysql_error());
			$arr2 = mysql_fetch_object($query2);
			return($arr2->P_grade);
			} else {
				return("X");	
			}
		}
		
		function findOneAfter()
		{
			$query1 = mysql_query('SELECT COUNT(`P_grade`) AS rowcount FROM `plan` WHERE `P_S_id` =  "'.$this->getSID().'" AND `P_year` <= "'.$this->getYear().'" AND `P_term` >= "'.$this->getTerm().'" AND `P_U_id` = "'.$this->getUID().'" ORDER BY `P_year` DESC, `P_term` DESC LIMIT 1')or die(mysql_error());
			$arr1 = mysql_fetch_object($query1);
			if($arr1->rowcount > 0)
			{
			$query2 = mysql_query('SELECT `P_grade` FROM `plan` WHERE `P_S_id` =  "'.$this->getSID().'" AND `P_year` <= "'.$this->getYear().'" AND `P_term` >= "'.$this->getTerm().'" AND `P_U_id` = "'.$this->getUID().'" AND `P_id` != "'.$this->getID().'" ORDER BY `P_year` DESC, `P_term` DESC LIMIT 1')or die(mysql_error());
			$arr2 = mysql_fetch_object($query2);
			return($arr2->P_grade);
			} else {
				return("X");	
			}
		}
		
		function findOnePlan(){
			$query = mysql_query('SELECT COUNT(`P_S_id`) AS rowcount FROM `plan` WHERE `P_year` = "'.$this->getYear().'" AND `P_U_id` = "1" AND `P_term` = '.$this->getTerm().' AND `P_S_id` = "'.$this->getSID().'"')or die(mysql_error());
			$arr = mysql_fetch_object($query);
			return($arr->rowcount);
		}
		
		function findUser()
		{
			include('teacher.php');
			$tea = new Teacher;
			include('deparment.php');
			$dep = new Deparment;
			include('group_subject.php');
			$gop = new Group_subject;
			include('subject.php');
			$sub = new Subject;
			$sub2 = new Subject;
			
			$query = mysql_query('SELECT P_id,P_S_id,P_U_id,P_year,P_term,P_grade,P_grade_plan FROM plan WHERE P_U_id = "'.$this->uId.'" ORDER BY `P_year`,`P_term`')or die(mysql_error());
			$data_array = array();
			$conut = 1;
			while($arr = mysql_fetch_object($query))
			{
				$data_array[$conut]['id'] = $arr->P_id;
				$data_array[$conut]['sid'] = $arr->P_S_id;
				$data_array[$conut]['uid'] = $arr->P_U_id;
				$data_array[$conut]['year'] = $arr->P_year;
				$data_array[$conut]['term'] = $arr->P_term;
				$data_array[$conut]['grade'] = $arr->P_grade;
				$data_array[$conut]['gradeP'] = $arr->P_grade_plan;
				$sub->setID($arr->P_S_id);
				$data_array[$conut]['sName'] = $sub->findOne();
				$data_array[$conut]['sNumber'] = $sub->getNember();
				$data_array[$conut]['sCredit'] = $sub->getCredit();
				$tea->setID($sub->getTID());
				$data_array[$conut]['tid'] = $tea->getID();
				$data_array[$conut]['tName'] = $tea->findOne();
				$gop->setID($sub->getGID());
				$data_array[$conut]['gid'] = $gop->getID();
				$data_array[$conut]['gName'] = $gop->findOne();
				$dep->setID($gop->getDID());
				$data_array[$conut]['did'] = $dep->getID();
				$data_array[$conut]['dName'] = $dep->findOne();
				if($sub->getSID() != 0)
				{
					$sub2->setID($sub->getSID());
					$data_array[$conut]['sPreId'] = $sub2->getID();
					$data_array[$conut]['sPreName'] = $sub2->findOne();
				}else
				{
					$data_array[$conut]['sPreId'] = "";
					$data_array[$conut]['sPreName'] = "NONE";	
				}
				$data_array[$conut]['detail'] = $sub->getDetail();
				$conut++;
			}
			return json_encode($data_array);
		}
		
		function findAll()
		{
			include('teacher.php');
			$tea = new Teacher;
			include('deparment.php');
			$dep = new Deparment;
			include('group_subject.php');
			$gop = new Group_subject;
			include('subject.php');
			$sub = new Subject;
			$sub2 = new Subject;
			
			$query = mysql_query('SELECT P_id,P_S_id,P_U_id,P_year,P_term,P_grade,P_grade_plan FROM plan ORDER BY `P_year`,`P_term`')or die(mysql_error());
			$data_array = array();
			$conut = 1;
			while($arr = mysql_fetch_object($query))
			{
				$data_array[$conut]['id'] = $arr->P_id;
				$data_array[$conut]['sid'] = $arr->P_S_id;
				$data_array[$conut]['uid'] = $arr->P_U_id;
				$data_array[$conut]['year'] = $arr->P_year;
				$data_array[$conut]['term'] = $arr->P_term;
				$data_array[$conut]['grade'] = $arr->P_grade;
				$data_array[$conut]['gradeP'] = $arr->P_grade_plan;
				$sub->setID($arr->P_S_id);
				$data_array[$conut]['sName'] = $sub->findOne();
				$data_array[$conut]['sNumber'] = $sub->getNember();
				$data_array[$conut]['sCredit'] = $sub->getCredit();
				$tea->setID($sub->getTID());
				$data_array[$conut]['tName'] = $tea->findOne();
				$gop->setID($sub->getGID());
				$data_array[$conut]['gName'] = $gop->findOne();
				$dep->setID($gop->getDID());
				$data_array[$conut]['dName'] = $dep->findOne();
				if($sub->getSID() != 0)
				{
					$sub2->setID($sub->getSID());
					$data_array[$conut]['sPreId'] = $sub2->getID();
					$data_array[$conut]['sPreName'] = $sub2->findOne();
				}else
				{
					$data_array[$conut]['sPreName'] ="NONE";	
				}
				$data_array[$conut]['detail'] = $sub->getDetail();
				$conut++;
			}
			return json_encode($data_array);
		}
		
		function maxYear()
		{
			$query = mysql_query('SELECT MAX(`P_year`) AS Max FROM `plan` WHERE P_U_id = "'.$this->uId.'"')or die(mysql_error());
			$arr = mysql_fetch_object($query);
			return($arr->Max);
		}
		
		function add()
		{
			mysql_query('INSERT INTO plan(P_S_id,P_U_id,P_year,P_term,P_grade,P_grade_plan) VALUES("'.$this->sId.'","'.$this->uId.'","'.$this->year.'","'.$this->term.'","'.$this->grade.'","'.$this->gradePlan.'")')or die(mysql_error());
		}
		
		function edit()
		{
			mysql_query('UPDATE plan SET P_S_id="'.$this->sId.'",P_U_id="'.$this->uId.'",P_year="'.$this->year.'",P_term="'.$this->term.'",P_grade="'.$this->grade.'",P_grade_plan="'.$this->gradePlan.'" WHERE P_id = "'.$this->id.'" LIMIT 1')or die(mysql_error());
		}
		
		function del()
		{
			mysql_query('DELETE FROM plan WHERE P_id="'.$this->id.'" LIMIT 1') or die(mysql_error());
		}
		
		function editGrade()
		{
			mysql_query('UPDATE plan SET P_grade="'.$this->grade.'" WHERE P_id = "'.$this->id.'" LIMIT 1')or die(mysql_error());	
		}
		
		function editGradeP()
		{
			mysql_query('UPDATE plan SET P_grade_plan="'.$this->gradePlan.'" WHERE P_id = "'.$this->id.'" LIMIT 1')or die(mysql_error());	
		}
		
	}
?>